package com.linkage.usercenter.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.usercenter.dao.CoreDptInfoMapper;
import com.linkage.usercenter.dao.MgrLoginDailyMapper;
import com.linkage.usercenter.pojo.entity.CoreDptInfo;
import com.linkage.usercenter.pojo.entity.MgrLoginDaily;
import com.linkage.usercenter.service.MgrLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class MgrLoginServiceImpl implements MgrLoginService {
    @Autowired
    private MgrLoginDailyMapper mgrLoginDailyMapper;
    @Autowired
    private CoreDptInfoMapper dptInfoMapper;

    @Override
    public List<MgrLoginDaily> queryMgrLoginDailyTotal(String token) {
        try{
            String dptCode = token.split(":")[1];
            if(StrUtil.equals("CJGLY-3",token.split(":")[2])){
                dptCode = "POLICE-0001";
            }
            //TODO 根据token查询所有子部门
            CoreDptInfo dptInfo = dptInfoMapper.queryByDptCode(dptCode).get(0);
            List<CoreDptInfo> coreDptInfos = new ArrayList<CoreDptInfo>();
            if(CommonSysConstant.ORG_CODE_TYPE_5.equals(dptInfo.getDptOrgCode())){
                //企业特殊处理
                this.getDptComList(dptInfo.getDptCode(), coreDptInfos);
            }else {
                coreDptInfos = getDptListAll(dptCode);
            }
            coreDptInfos.add(dptInfo);
            int day1Login=0;
            int day1Nologin=0;
            int day2Login=0;
            int day2Nologin=0;
            int day3Login=0;
            int day3Nologin=0;
            int day4Login=0;
            int day4Nologin=0;
            Integer today = Integer.parseInt(DateUtil.format(new Date(),"yyyyMMdd"));
            Integer yesterday = Integer.parseInt(DateUtil.format(DateUtil.yesterday().toJdkDate(),"yyyyMMdd"));
            Integer beThreeDay = Integer.parseInt(DateUtil.format(DateUtil.offsetDay(new Date(),-2).toJdkDate(),"yyyyMMdd"));
            Integer befourDay = Integer.parseInt(DateUtil.format(DateUtil.offsetDay(new Date(),-3).toJdkDate(),"yyyyMMdd"));
            for(CoreDptInfo dptInfo1 : coreDptInfos){
                //今日登陆人数
                List<MgrLoginDaily> mgrLoginDailyList = mgrLoginDailyMapper.queryByDateAndDptCode(today,dptInfo1.getDptCode());
                if(ObjectUtil.isNotEmpty(mgrLoginDailyList) && mgrLoginDailyList.size() > 0){
                    day1Login += mgrLoginDailyList.get(0).getLoginCount();
                    day1Nologin += mgrLoginDailyList.get(0).getNoLoginCount();
                }
                //昨日登陆人数
                List<MgrLoginDaily> mgrLoginDailyList1 = mgrLoginDailyMapper.queryByDateAndDptCode(yesterday,dptInfo1.getDptCode());
                if(ObjectUtil.isNotEmpty(mgrLoginDailyList1) && mgrLoginDailyList1.size() > 0){
                    day2Login += mgrLoginDailyList1.get(0).getLoginCount();
                    day2Nologin += mgrLoginDailyList1.get(0).getNoLoginCount();
                }
                //三日钱登陆人数
                List<MgrLoginDaily> mgrLoginDailyList2 = mgrLoginDailyMapper.queryByDateAndDptCode(beThreeDay,dptInfo1.getDptCode());
                if(ObjectUtil.isNotEmpty(mgrLoginDailyList2) && mgrLoginDailyList2.size() > 0){
                    day3Login += mgrLoginDailyList2.get(0).getLoginCount();
                    day3Nologin += mgrLoginDailyList2.get(0).getNoLoginCount();
                }
                //四日前登陆人数
                List<MgrLoginDaily> mgrLoginDailyList3 = mgrLoginDailyMapper.queryByDateAndDptCode(befourDay,dptInfo1.getDptCode());
                if(ObjectUtil.isNotEmpty(mgrLoginDailyList3) && mgrLoginDailyList3.size() > 0){
                    day4Login += mgrLoginDailyList3.get(0).getLoginCount();
                    day4Nologin += mgrLoginDailyList3.get(0).getNoLoginCount();
                }
            }
            List<MgrLoginDaily> result = new ArrayList<>();
            result.add(MgrLoginDaily.builder().noLoginCount(day1Nologin).loginCount(day1Login).loginDate(today).build());
            result.add(MgrLoginDaily.builder().noLoginCount(day2Nologin).loginCount(day2Login).loginDate(yesterday).build());
            result.add(MgrLoginDaily.builder().noLoginCount(day4Nologin).loginCount(day3Login).loginDate(beThreeDay).build());
            result.add(MgrLoginDaily.builder().noLoginCount(day4Nologin).loginCount(day4Login).loginDate(befourDay).build());
            result.sort(new Comparator<MgrLoginDaily>() {
                @Override
                public int compare(MgrLoginDaily o1, MgrLoginDaily o2) {
                    if(o1.getLoginDate()>o2.getLoginDate()){
                        return 1;
                    }else if(o1.getLoginDate()<o2.getLoginDate()){
                        return -1;
                    }
                    return 0;
                }
            });
            return result;
        }catch (Exception e){
            log.error("",e);
        }
        return null;
    }
    
    //根据机构代码查询机构信息
    private void getDptComList(String dptCode, List<CoreDptInfo> coreDptInfos) {
        if(StrUtil.isBlank(dptCode)){
            return;
        }
        List<String>  dptCodeList = dptInfoMapper.queryCodeByParentCode(dptCode);
        if(dptCodeList == null || dptCodeList.isEmpty()){
            return;
        }
        List<CoreDptInfo>  dptDOList = dptInfoMapper.queryByCodeList(dptCodeList);
        if(dptDOList == null || dptDOList.isEmpty()){
            return;
        }
        coreDptInfos.addAll(dptDOList);
        for (CoreDptInfo dptDO : dptDOList) {
            this.getDptComList(dptDO.getDptCode(), coreDptInfos);
        }
    }

    private List<CoreDptInfo> getDptListAll(String dptCode){
        List<CoreDptInfo> result = new ArrayList<>();
        List<CoreDptInfo> list = dptInfoMapper.queryByParentCode(dptCode);
        if(ObjectUtil.isNotEmpty(list)){
            result.addAll(list);
            for(CoreDptInfo dptInfo : list){
                result.addAll(getDptListAll(dptInfo.getDptCode()));
            }
        }
        return result;
    }
}
